package com.dmcb.trade.business.crawlers.article;

import com.dmcb.common.business.utils.DateUtil;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;

/**
 * 极客公园网站解析
 * Created by 黑米 on 2017/3/8.
 */
@Service
public class JiKeGongYuanParser  extends BaseParser {

    /**
     * 网站URL网址
     *
     * @return 链接
     */
    @Override
    protected String url() {
        return CrawlConstant.JIKEGONGYUAN_URL;
    }

    /**
     * 获取标题选择器
     *
     * @return 标题选择器
     */
    @Override
    protected String titleSelect() {
        return "h1.topic-title";
    }

    /**
     * 获取日期选择器
     *
     * @return 发布日期选择器
     */
    @Override
    protected String dateSelect() {
        return "span.release-date>span[data-time]";
    }

    @Override
    protected void setPostDate(Document doc, Article article) {
        Element data = doc.select(dateSelect()).first();
        if(data == null){
            return;
        }
        Date time = null;
        try {
            time = DateUtil.stampToDate(data.attr("data-time"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        article.setPostTime(time);
    }

    /**
     * 获取正文内容选择器
     *
     * @return 正文内容选择器
     */
    @Override
    protected String bodySelect() {
        return "div.article-content";
    }

    /**
     * 获取封面选择器
     *
     * @return 封面选择器
     */
    @Override
    protected String coverSelect() {
        return "div.topic-cover img[src]";
    }

    /**
     * 获取作者选择器
     *
     * @return 作者选择器
     */
    @Override
    protected String authorNameSelect() {
        return "a.author>span";
    }

    @Override
    protected boolean isProxyEnabled() {
        return true;
    }

    @Override
    protected String platform() {
        return "极客公园";
    }
}
